package com.wnx.job.modules.rms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.mapstruct.TrainMapper;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.service.PmsResumeService;
import com.wnx.job.modules.rms.dto.ResumeParam;
import com.wnx.job.modules.rms.dto.TrainParam;
import com.wnx.job.modules.rms.model.RmsTrain;
import com.wnx.job.modules.rms.service.RmsTrainService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/rms/rmsTrain")
public class RmsTrainController {
    @Resource
    private JmsStudentService studentService;
    @Resource
    private PmsResumeService resumeService;
    @Resource
    private RmsTrainService trainService;

    /**
     * 新增培训经历
     * @param trainParam
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增培训经历")
    public ResultData<String> save(@RequestBody @Validated TrainParam trainParam, Principal principal){
        log.info("=======系统正在新增培训经历======");
        //获取前端的培训经历，属性复制
        RmsTrain tbTrain = TrainMapper.INSTANCE.toPojo(trainParam);


        //设置简历ID进培训经历
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        tbTrain.setResumeId(resume.getId());

        //调用服务层，进行新增培训经历
        trainService.save(tbTrain);
        log.info("=======系统正在新增培训经历成功！======");
        return ResultData.success(Const.ADD_TRAIN_SUCCESS);
    }

    /**
     * 删除培训经历
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除培训经历")
    public ResultData<String> deleteTrain(@PathVariable(name = "id")Long id){
        log.info("========系统正在删除培训经历=====");
        trainService.removeById(id);
        log.info("========系统删除培训经历成功！=====");
        return ResultData.success(Const.DELETE_TRAIN_SUCCESS);
    }


    /**
     * 查询培训经历
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("查询培训经历")
    public ResultData<RmsTrain> findById(@PathVariable(name = "id")Long id){
        log.info("========系统正在查询培训经历=====");
        RmsTrain train = trainService.getById(id);
        log.info("========系统查询培训经历成功！=====");
        return ResultData.success(Const.QUERY_PROJECT_SUCCESS,train);

    }


    /**
     * 修改培训经历
     * @param trainParam
     * @param principal
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改培训经历")
    public ResultData<String> update(@Valid @RequestBody TrainParam trainParam, Principal principal){
        log.info("========系统正在修改培训经历！=====");
        //获取前端的培训经历

        //属性复制
        RmsTrain tbTrain = TrainMapper.INSTANCE.toPojo(trainParam);

        //设置简历ID进培训经历
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        tbTrain.setResumeId(resume.getId());

        //调用服务层，进行修改培训经历
        trainService.updateById(tbTrain);

        log.info("========系统正在修改培训经历成功！！！=====");
        //返回结果集
        return ResultData.success(Const.UPDATE_TRAIN_SUCCESS);


    }


    /**
     * 查询全部培训经历
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部培训经历")
    public ResultData<List<RmsTrain>> findAll(Principal principal){
        log.info("=====系统正在查询全部培训经历=====");
        //通过认证主体，获取学生学号。
        String account = principal.getName();

        //调用服务层，根据学生学号获取学生信息
        JmsStudent student = studentService.findByAccount(account);

        //调用服务层，根据学生ID获取简历
        ResumeParam resume = resumeService.findByStudentId(student.getId());

        //调用服务层进行该学生培训经历
        List<RmsTrain> trainList = trainService.findTrainListByResumeId(resume.getId());

        log.info("=====系统正在查询全部培训经历成功！=====");
        //返回结果集对象
        return ResultData.success(Const.QUERY_TRAIN_SUCCESS,trainList);
    }




}

